package xyz.fuqufyb.aec.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.fuqufyb.aec.data.JsonResult;
import xyz.fuqufyb.aec.data.TableResult;
import xyz.fuqufyb.aec.entity.ApiMapping;
import xyz.fuqufyb.aec.service.IApiMappingService;

import java.util.Arrays;
import java.util.Map;

/**
 * @author 用户: fyb
 * @description class: ApiMappingController
 * @date 时间: 2021/2/2 16:51
 */
@RestController
@RequestMapping("/sys/api_mapping")
public class ApiMappingController {



    @Autowired
    private IApiMappingService apiMappingService;

    /**
     * 分页加模糊查询
     * @param maps
     * @return
     */
    @RequestMapping("/table")
    public TableResult getTable(@RequestParam Map<String, Object> maps) {
        try {
            TableResult pageApiMapping = apiMappingService.getPageApiMapping(maps);
            return pageApiMapping;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return TableResult.getTableResultError();

    }

    /**
     * 修改和新增
     * @param apiMapping
     * @return
     */
    @PostMapping("/update")
    public JsonResult updateApiMapping(ApiMapping apiMapping) {
        Integer id = apiMapping.getId();
        if (id == null) {
            int i = apiMappingService.insertSelective(apiMapping);
            if (i > 0) {
                return JsonResult.getJsonSuccess("新增成功");
            } else {
                return JsonResult.getJsonError();
            }
        } else {
            int i = apiMappingService.updateByPrimaryKeySelective(apiMapping);

            if (i > 0) {
                return JsonResult.getJsonSuccess("修改成功");
            } else {

                return JsonResult.getJsonError();
            }
        }

    }
    /**
     *  批量删除 路由
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public JsonResult delApiMapping(@RequestParam("ids") Integer[] ids) {
        int i = apiMappingService.delApiMappingIds(Arrays.asList(ids));
        if (i > 0) {
            return JsonResult.getJsonSuccess("删除成功");
        }
        return JsonResult.getJsonError();

    }

}
